import http from '../../utils/http';
import { tcbAuth, tcbDB } from '../../tcb';

const todo = {
  state:{
    isTcbLogin: false,
    isToDoEidt: false,
    isToDoEditItemVisible: false,
    currentToDoList: [],
    currentEditToDo: {}
  },
  mutations:{
    toggleIsTcbLogin(state, isTcbLogin){
      state.isTcbLogin = typeof isTcbLogin === 'undefined' ? !state.isTcbLogin : isTcbLogin;
    },
    toggleIsToDoEidt(state, isToDoEidt){
      state.isToDoEidt = typeof isToDoEidt === 'undefined' ? !state.isToDoEidt : isToDoEidt;
    },
    toggleIsToDoEditItemVisible(state, isToDoEditItemVisible){
      state.isToDoEditItemVisible = typeof isToDoEditItemVisible === 'undefined' ? !state.isToDoEditItemVisible : isToDoEditItemVisible;
    },
    updateCurrentToDoList(state, currentToDoList){
      state.currentToDoList = currentToDoList;
    },
    updateCurrentEditToDo(state, currentEditToDo){
      state.currentEditToDo = currentEditToDo;
    },
    resetCurrentEditToDo(state){
      state.currentEditToDo = {};
    },
    reset(state){
      Object.assign(state, {
        isTcbLogin: false,
        isToDoEidt: false,
        isToDoEditItemVisible: false,
        currentToDoList: [],
        currentEditToDo: {}
      });
    }
  },
  actions:{
    login(context,userID){
      http.getTcbTicket(
        JSON.stringify({ 'userID': userID })
      ).then(tcbTicket => {
        context.dispatch('tcbLogin', { userID, tcbTicket });
      }).catch(() =>{
        context.commit('showMessage', { message: '云开发鉴权获取失败，请联系作者修复！微信号：LeeYu2333' , type: 'error' });
      });;
    },
    logout(context){
      const loginState = tcbAuth.hasLoginState();
      if(!loginState){
        tcbAuth.signOut().catch(error => {
          context.commit('showMessage', { message: '云开发登出失败' + error , type: 'error' });
        });
      }
    },
    tcbLogin(context, { userID, tcbTicket }){
      const loginState = tcbAuth.hasLoginState();
      if(!loginState){
        tcbAuth.customAuthProvider().signIn(tcbTicket).then(() =>{
          context.commit('toggleIsTcbLogin', true);
          context.dispatch('getToDoList', userID);
        }).catch(error =>{
          context.commit('showMessage', { message: '云开发登录失败' + error , type: 'error' });
        });
      } else {
        context.commit('toggleIsTcbLogin', true);
        context.dispatch('getToDoList', userID);
      }
    },
    getToDoList(context, userID){
      tcbDB.collection('todo').where({
        userID
      }).get().then(res => {
        context.commit('updateCurrentToDoList', res.data);
      }).catch(error => {
        context.commit('showMessage', { message: '获取云待办失败' + error , type: 'error' });
      });
    },
    addToDo(context, todo){
      tcbDB.collection('todo').add(todo).then(()=>{
        context.dispatch('getToDoList', todo.userID);
        context.commit('toggleIsToDoEditItemVisible', false);
      }).catch(error => {
        context.commit('showMessage', { message: '新增云待办失败' + error , type: 'error' });
      });
    },
    editToDo(context, todo){
      // 修改时不能传_id和_openid
      let _id = todo._id;
      delete todo._openid;
      delete todo._id;
      tcbDB.collection('todo').doc(_id).update(todo).then(() => {
        context.dispatch('getToDoList', todo.userID);
        context.commit('showMessage', { message: '编辑云待办成功' , type: 'success' });
        context.commit('toggleIsToDoEditItemVisible', false);
      }).catch(error => {
        context.commit('showMessage', { message: '编辑云待办失败' + error , type: 'error' });
      });
    },
    removeToDo(context, todo){
      tcbDB.collection('todo').doc(todo._id).remove().then(() => {
        context.dispatch('getToDoList', todo.userID);
        context.commit('showMessage', { message: '删除云待办成功' , type: 'success' });
      }).catch(error => {
        context.commit('showMessage', { message: '获取云待办失败' + error , type: 'error' });
      });
    }
  }
}

export default todo;